package com.base.lib.xdialog.utils

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder

abstract class OnItemNoDoubleClickListener: OnItemClickListener {

    private var lastClickTime: Long = 0

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > 500L) {
            lastClickTime = currentTime
            onNoDoubleClick(adapter, view, position)
        }
    }

    protected abstract fun onNoDoubleClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int)
}

fun <T, K : BaseViewHolder> BaseQuickAdapter<T, K>.onItemDebouncedClick(
    click: (adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) -> Unit
) {
    setOnItemClickListener(object : OnItemNoDoubleClickListener() {
        override fun onNoDoubleClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
            click(adapter, view, position)
        }
    })
}
